﻿Imports FinanceiroDAL
Imports FinanceiroBLL
Imports System.Text.RegularExpressions
Public Class CadastroContasFRM
    Dim objBLL As New ContasBLL
    Dim operacao As String
    Private Const EmptySpace As String = " "
#Region "Construtor"
    Public Sub New(ByVal codigo As Integer)
        InitializeComponent()
        objBLL = New ContasBLL(codigo)
        carrega_campos()
        operacao = ""
    End Sub
    Public Sub New()
        InitializeComponent()
    End Sub
#End Region
#Region "CarregaCampos"
    Sub carrega_campos()
        TCodigo.Text = objBLL.codigo
        TConta.Text = objBLL.descricao
        'If (objBLL.banco <> Nothing) Then
        CBBanco.SelectedValue = objBLL.banco
        ' End If
        DTPAbertura.Text = objBLL.dtabertura
        DTPFechamento.Text = objBLL.dtfechamento
        TAgencia.Text = objBLL.agencia
        TContaCorrente.Text = objBLL.contacorrente
        TGerente.Text = objBLL.gerente
        TEmail.Text = objBLL.email
        TSaldoInicial.Text = objBLL.saldoinicial
        CBStatus.SelectedValue = objBLL.status
        If (objBLL.tipoConta = "B") Then
            CBTpConta.SelectedIndex = 0
            Visible = True
            LAgencia.Visible = True
            LConta.Visible = True
            LSaldoIn.Visible = True
            LGerente.Visible = True
            LEmail.Visible = True
            TAgencia.Visible = True
            TContaCorrente.Visible = True
            TSaldoInicial.Visible = True
            TGerente.Visible = True
            TEmail.Visible = True
        Else
            CBTpConta.SelectedIndex = 1
            LAgencia.Visible = False
            LConta.Visible = False
            ' LSaldoIn.Visible = False
            LGerente.Visible = False
            LEmail.Visible = False
            TAgencia.Visible = False
            TContaCorrente.Visible = False
            'TSaldoInicial.Visible = False
            TGerente.Visible = False
            TEmail.Visible = False
        End If

    End Sub
#End Region
#Region "Carrega propriedades"
    Sub CarregaPropriedades()
        objBLL.limpar()
        If (TCodigo.Text <> "") Then
            objBLL.codigo = TCodigo.Text
        Else
            objBLL.codigo = 0
        End If
        objBLL.descricao = Trim(TConta.Text)
        objBLL.banco = CBBanco.SelectedValue
        objBLL.dtabertura = DTPAbertura.Text
        If DTPFechamento.Text = EmptySpace Then
            Me.DTPFechamento.CustomFormat = " "
        Else
            objBLL.dtfechamento = DTPFechamento.Text
        End If
        If (TAgencia.Text <> Nothing) Then
            objBLL.agencia = Val(TAgencia.Text)
        Else
            objBLL.agencia = 0
        End If
        If (TContaCorrente.Text <> Nothing) Then
            objBLL.contacorrente = Val(TContaCorrente.Text)
        Else
            objBLL.contacorrente = 0
        End If

        If (TGerente.Text <> Nothing) Then
            objBLL.gerente = TGerente.Text
        Else
            objBLL.gerente = ""
        End If
        If (TEmail.Text <> Nothing) Then
            objBLL.email = TEmail.Text
        Else
            objBLL.email = ""
        End If
        If (TSaldoInicial.Text <> "") Then
            objBLL.saldoinicial = (Trim(TSaldoInicial.Text)).Replace("R$", "")
        Else
            objBLL.saldoinicial = 0
        End If
        objBLL.status = CBStatus.SelectedValue
        ' objBLL.tipoConta = CBTpConta.SelectedValue
        If (CBTpConta.SelectedItem = "Bancária") Then
            objBLL.tipoConta = "Bancária"
        Else
            objBLL.tipoConta = "Não Bancária"
        End If
    End Sub
#End Region
#Region "Limpar"
    Sub limpar()
        TCodigo.Text = Nothing
        TConta.Text = Nothing
        CBBanco.SelectedItem = ""
        DTPAbertura.Text = Nothing
        DTPAbertura.Text = Nothing
        TAgencia.Text = Nothing
        TContaCorrente.Text = Nothing
        TSaldoInicial.Text = Nothing
        TGerente.Text = Nothing
        TEmail.Text = Nothing
        CBStatus.SelectedItem = ""
        CBTpConta.SelectedItem = ""
    End Sub
#End Region
#Region "Habilita"
    Sub Habilita()
        If (CBTpConta.SelectedItem = "Não bancária") Then
            LAgencia.Visible = False
            LConta.Visible = False
            ' LSaldoIn.Visible = False
            LGerente.Visible = False
            LEmail.Visible = False
            TAgencia.Visible = False
            TContaCorrente.Visible = False
            'TSaldoInicial.Visible = False
            TGerente.Visible = False
            TEmail.Visible = False
            TConta.Focus()
            objconexao.carregaCombo(CBBanco, "Financeiro.GEn_Bancos", "ban_codigo", "ban_descricao", "ban_tipo='Não Bancária'", "")
        Else
            Visible = True
            LAgencia.Visible = True
            LConta.Visible = True
            LSaldoIn.Visible = True
            LGerente.Visible = True
            LEmail.Visible = True
            TAgencia.Visible = True
            TContaCorrente.Visible = True
            TSaldoInicial.Visible = True
            TGerente.Visible = True
            TEmail.Visible = True
            objconexao.carregaCombo(CBBanco, "Financeiro.GEn_Bancos", "ban_codigo", "ban_descricao", "ban_tipo='Bancária'", "")
        End If
    End Sub
#End Region
#Region "Load"
    Private Sub CadastroContasFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ' CBTpConta.SelectedIndex = 1
        objconexao.carregaCombo(CBStatus, "Generico.Gen_Status", "sta_codigo", "sta_descricao", "sta_codigo=1 or sta_codigo=2", "sta_descricao")
        objconexao.carregaCombo(CBBanco, "Financeiro.GEn_Bancos", "ban_codigo", "ban_descricao", "ban_tipo='Bancária'", "")
        Me.DTPFechamento.Format = DateTimePickerFormat.Custom
        Me.DTPFechamento.CustomFormat = " "
        CBStatus.SelectedValue = 1
        CBTpConta.SelectedValue = -1
        CBBanco.SelectedValue = -1
        TSaldoInicial_Leave(Nothing, Nothing)
        If (Not IsNothing(TCodigo.Text) And TCodigo.Text <> "") Then
            objBLL = New ContasBLL(Val(TCodigo.Text))
            carrega_campos()
            operacao = "A"
        Else
            operacao = "I"
            DTPFechamento.Enabled = False
        End If
        CBTpConta.Select()
    End Sub
#End Region
#Region "Salvar"
    Private Sub TSBSalvar_Click(sender As System.Object, e As System.EventArgs) Handles TSBSalvar.Click
        Try
            'carregar campos
            ' objBLL = New ContasBLL(Val(TCodigo.Text), TConta.Text, CBStatus.SelectedValue)
            CarregaPropriedades()
            If operacao = "I" Then
                If (objBLL.inserir()) Then
                    MessageBox.Show("Conta inserida com sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    limpar()
                    Me.Close()
                Else : MessageBox.Show("A Conta não pôde ser inserida", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
                ' End If 'tírar
            Else
                If (objBLL.Alterar()) Then
                    MessageBox.Show("Conta alterada com sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    limpar()
                    Me.Close()
                Else : MessageBox.Show("A Conta não pôde ser alterada", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub
#End Region
#Region "Propriedades"
    Private Sub CBTpConta_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBTpConta.SelectedIndexChanged
        Habilita()
    End Sub
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
        limpar()
    End Sub
    Private Sub TSaldoInicial_Validated(sender As System.Object, e As System.EventArgs)
        TSaldoInicial.Text = FormatCurrency(TSaldoInicial.Text)
    End Sub
    Private Sub TConta_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs)
        If Not Char.IsLetter(e.KeyChar) And Not e.KeyChar = "." And Not e.KeyChar = "," And Not e.KeyChar = vbBack And e.KeyChar = "" Then
            e.Handled = True
        End If
    End Sub
    Private Sub TAgencia_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs)
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = "-" And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub
    Private Sub TContaCorrente_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs)
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = "-" And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub
    Private Sub TSaldoInicial_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs)
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
    End Sub
    Private Sub TGerente_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs)
        If Not Char.IsLetter(e.KeyChar) And e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," And e.KeyChar = "" Then
            e.Handled = True
        End If
    End Sub
#End Region
#Region "Status Selected index"
    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs)
        If CBStatus.SelectedIndex = 0 Then
            DTPFechamento.Visible = False
            LFechamento.Visible = False
        Else
            LFechamento.Visible = True
            DTPFechamento.Visible = True
        End If
    End Sub
#End Region
#Region "DTPFechamento"
    Private Sub DTPFechamento_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DTPFechamento.ValueChanged
        ' Verifica se o texto é um espaço em branco
        If DTPFechamento.Text = EmptySpace Then
            ' Define o formato como Shot (formato DD-MM-YYYY) e dá um
            ' enter para fechar (CloseUp) da janela que está aberta
            Me.DTPFechamento.Format = DateTimePickerFormat.Short
            SendKeys.Send("{ENTER}")
        End If
    End Sub
#End Region
#Region "DTPFechamento"
    Private Sub DTPFechamento_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DTPFechamento.KeyPress
        If DTPFechamento.CustomFormat <> "" Then
            CBStatus.SelectedIndex = 1
        Else
            CBStatus.SelectedIndex = 2
        End If
    End Sub
#End Region
#Region "Leave"
    Private Sub TSaldoInicial_Leave(sender As System.Object, e As System.EventArgs) Handles TSaldoInicial.Leave
        If TSaldoInicial.Text <> "" Then
            TSaldoInicial.Text = FormatCurrency(TSaldoInicial.Text)
        Else
            TSaldoInicial.Text = "0,00"
        End If
    End Sub
#End Region

    Private Sub TSaldoInicial_KeyPress_1(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TSaldoInicial.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            TSaldoInicial_Leave(Nothing, Nothing)
        End If
    End Sub

End Class